#ifndef STX_ALGORITHM_ADJACENT_DIFFERENCE_HPP
#define STX_ALGORITHM_ADJACENT_DIFFERENCE_HPP

#include <stx/algorithm/common.hpp>

namespace stx {

template <class InputIterator, class OutputIterator>
inline OutputIterator
adjacent_difference(InputIterator first, InputIterator last, OutputIterator result)
{
    if (first != last) {
        typename std::iterator_traits<InputIterator>::value_type t1(*first);
        *result = t1;
        for (++first, ++result; first != last; ++first, ++result) {
            typename std::iterator_traits<InputIterator>::value_type t2(*first);
            *result = t2 - t1;
            t1 = t2;
        }
    }
    return result;
}

template <class InputIterator, class OutputIterator, class BinaryOperation>
inline OutputIterator
adjacent_difference(
    InputIterator first,
    InputIterator last,
    OutputIterator result,
    BinaryOperation binary_op)
{
    if (first != last) {
        typename std::iterator_traits<InputIterator>::value_type t1(*first);
        *result = t1;
        for (++first, ++result; first != last; ++first, ++result) {
            typename std::iterator_traits<InputIterator>::value_type t2(*first);
            *result = binary_op(t2, t1);
            t1 = t2;
        }
    }
    return result;
}

} // namespace stx

#endif // STX_ALGORITHM_ADJACENT_DIFFERENCE_HPP
